package com.chao.lease.web.admin.custom.converter;

import com.chao.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String s) {

        ItemType[] itemTypes = ItemType.values();  // 获取所有的枚举

        for (ItemType itemType : itemTypes) {  // 遍历匹配并返回
            if (itemType.getCode().equals(Integer.valueOf(s))){
                return itemType;
            }
        }
        throw new IllegalArgumentException("code:" + s + "非法");
    }
}
